package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service

public class DestinationServiceImpl implements IDestinationService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private DestinationRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IRegionService regionService;

    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        Destination destination1 = repository.findById(destination.getId()).get();
        destination1.setChildren(destination.getChildren());
        destination1.setCoverUrl(destination.getCoverUrl());
        destination1.setDeep(destination.getDeep());
        destination1.setEnglish(destination.getEnglish());
        destination1.setInfo(destination.getInfo());
        destination1.setParentId(destination.getParentId());
        destination1.setParentName(destination.getParentName());
        repository.save(destination1);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        Destination destination = repository.findById(id).get();
        return destination;
    }

    @Override
    public List<Destination> list() {
        List<Destination> destinations = repository.findAll();
        return destinations;
    }

    @Override
    public List<Destination> getDestByRegionId(String id) {
        //通过区域ID查询区域对象
        Region region = regionService.get(id);
        // 获取对象挂载得目的地集合
        List<String> refIds = region.getRefIds();
        //通过id集合查询目的地集合
        List<Destination> list= repository.findByIdIn(refIds);
        return list;
    }
    @Override
    public Page<Destination> query(DestinationQuery qo){
        Query query=new Query();
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        long count = mongoTemplate.count(query,Destination.class);
        if (count==0){
            return Page.empty();
        }
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Destination> list = mongoTemplate.find(query, Destination.class);
        return new PageImpl<>(list,pageable,count);
    }



    @Override
    public Object queryToasts(String parentId) {

        List<Destination> list=new ArrayList<>();

        //递归
        this.createToast(list,parentId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public Object queryDestByRegionIdForApi(String regionId) {
        List<Destination> list=new ArrayList<>();
        //国内
        if ("-1".equals(regionId)){
            list=repository.findByParentName("中国");
        }else {
            //非国内
            list=this.getDestByRegionId(regionId);
        }
        //第二层数据
        for (Destination dest:list){
           //约定查询子目的地只需要查询前5个
            Pageable pageable=PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> children= repository.findByParentId(dest.getId(),pageable);
           dest.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination queryName(String keyword) {
        Destination destination=repository.findByName(keyword);
        return destination;
    }

    /**
     *  用于创建toast对象
     *  @param list
     * @param parentId
     */
    private void createToast(List<Destination> list, String parentId) {
        //parentID为null时不继续执行
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        Destination dest = this.get(parentId);
        list.add(dest);
        if (StringUtils.hasLength(dest.getParentId())){
            createToast(list,dest.getParentId());
        }
    }
}






















